﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using bba.framework.infraweb.baseinfra;

namespace ibba.src.noriskbo.Web
{
    public partial class _default : BasePage //System.Web.UI.Page
    {
        private StringBuilder _parameters;

        public string SilverlightInitParams { get; set; }

        public _default()
        {
            base.CompressViewState = true;            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadUserParameters();
            this.SilverlightInitParams = _parameters.ToString();
        }

        private void LoadUserParameters()
        {
            _parameters = new StringBuilder();

            if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
            {                
                //string[] a = Context.User.Identity.Name.Split('\\');
                //System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);

                //this.SilverlightInitParams = string.Format("User.Identity.Name={0},Server.MachineName={1},IdSistema={2},FullName={3},XML_Servidores={4}",
                //                                     User.Identity.Name, Server.MachineName,
                //                                     System.Configuration.ConfigurationManager.AppSettings["IdSistema"],
                //                                     ADEntry.Properties["FullName"].Value.ToString(),
                //                                     System.Configuration.ConfigurationManager.AppSettings["XML_Servidores"]);

                string[] identity = Request.LogonUserIdentity.Name.Split('\\'); // Context.User.Identity.Name.Split('\\');

                string value="";

                try
                {
                    value = "";
                    value = identity.Length > 0 ? identity[0] : "";
                }
                finally
                {
                    AddParameter("Domain", value);
                }

                try
                {
                    value = "";
                    value = User.Identity.Name;
                }
                finally
                {
                    AddParameter("UserName", value);
                }

                try
                {
                    System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + identity[0] + "/" + identity[1]);

                    value = "";
                    value= ADEntry.Properties["FullName"].Value.ToString();
                }
                finally
                {
                    AddParameter("FullName", value);
                }
                                
                try
                {
                    value = "";
                    value = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName;
                }
                finally
                {
                    AddParameter("UserHostName", value);
                }

                try
                {
                    value = "";
                    value = Request.ServerVariables["REMOTE_ADDR"].ToString();
                }
                finally
                {
                    AddParameter("UserHostAddress", value);
                }

                
                try
                {
                    value = "";
                    value = System.Net.Dns.GetHostName(); //Server.MachineName;
                }
                finally
                {
                    AddParameter("WebServer", value);
                }
                
                AddParameter("IdSistema", System.Configuration.ConfigurationManager.AppSettings["IdSistema"]);
                AddParameter("XML_Servidores", System.Configuration.ConfigurationManager.AppSettings["XML_Servidores"]);
                
            }
        }

        private void AddParameter(string key, string value)
        {
            if (_parameters.Length > 0) 
                _parameters.Append(",");

            _parameters.Append(string.Concat(key, "=", value));
        }
    }
}